using System.Text.RegularExpressions;
using System.Web.Mvc;
using WroxBlog.Models;
using WroxBlog.Models.Interfaces;

namespace WroxBlog.Controllers
{
    public class PostController : Controller
    {
        private IPostRepository _postRepository;
        private ICategoryRepository _categoryRepository;
        private ICommentRepository _commentRepository;

        public PostController(IPostRepository postRepository,
            ICategoryRepository categoryRepository, ICommentRepository commentRepository)
        {
            this._postRepository = postRepository;
            this._categoryRepository = categoryRepository;
            this._commentRepository = commentRepository;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult IndividualPost(int id)
        {
            Post post = this._postRepository.GetPost(id);

            ViewData["Title"] = post.Title;

            ViewData["Categories"] = this._categoryRepository.GetCategories();
            ViewData["Post"] = post;
            ViewData["Comments"] = this._commentRepository.GetComments(id);

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult IndividualPost(int id, Comment comment)
        {
            // Validate Name
            if (string.IsNullOrEmpty(comment.AuthorName))
                ModelState.AddModelError("AuthorName",
                    "You must enter your name.");

            // Validate Email
            if (string.IsNullOrEmpty(comment.AuthorEmail))
                ModelState.AddModelError("AuthorEmail",
                    "You must enter your email.");
            else
            {
                Regex regex = new Regex
                    (@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
                    RegexOptions.IgnoreCase);

                if (!regex.IsMatch(comment.AuthorEmail))
                    ModelState.AddModelError("AuthorEmail",
                        "You must enter a valid email.");
            }

            // Validate URL
            if (string.IsNullOrEmpty(comment.AuthorUrl))
                ModelState.AddModelError("AuthorUrl",
                    "You must enter your URL.");
            else
            {
                Regex regex = new Regex
                    (@"(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)",
                    RegexOptions.IgnoreCase);

                if (!regex.IsMatch(comment.AuthorUrl))
                    ModelState.AddModelError("AuthorUrl",
                        "You must enter a valid URL.");
            }

            // Validate Body
            if (string.IsNullOrEmpty(comment.Body))
                ModelState.AddModelError("Body",
                    "You must enter the body text.");

            if (ViewData.ModelState.IsValid)
            {
                comment.PostID = id;
                this._commentRepository.AddComment(comment);
                return RedirectToAction("IndividualPost");
            }
            else
            {
                Post post = this._postRepository.GetPost(id);

                ViewData["Title"] = post.Title;

                ViewData["Categories"] = this._categoryRepository.GetCategories();
                ViewData["Post"] = post;
                ViewData["Comments"] = this._commentRepository.GetComments(id);

                return View();
            }
        }
    }
}
